package com.budwk.app.oss.services.impl;

import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.budwk.app.exception.ServiceException;
import com.budwk.app.oss.config.HuaweiyunOss;
import com.budwk.app.oss.models.SysOss;
import com.budwk.app.oss.services.SysOssService;
import com.budwk.starter.common.page.Pagination;
import com.budwk.starter.database.service.BaseServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.nutz.dao.Condition;
import org.nutz.dao.Dao;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.upload.TempFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.Date;
import java.util.List;

@Slf4j
@IocBean(args = {"refer:dao"})
public class SysOssServiceImpl extends BaseServiceImpl<SysOss> implements SysOssService {

    @Inject
    private HuaweiyunOss huaweiyunOss;

    public SysOssServiceImpl(Dao dao) {
        super(dao);
    }

    @Override
    public Pagination queryPageList(int pageNumber, int pageSize, Condition cnd) {
        return null;
    }

    @Override
    public List<SysOss> listByIds(Collection<Long> ossIds) {
        return null;
    }

    @Override
    public SysOss getById(Long ossId) {
        return null;
    }

    @Override
    public SysOss upload(TempFile file) {
        String originalFileName = file.getSubmittedFileName();
        String suffix = StringUtils.substring(originalFileName, originalFileName.lastIndexOf("."), originalFileName.length());
        String fileName = HuaweiyunOss.getObjectKey(suffix);
        //上传文件
        String url = huaweiyunOss.upload(file.getFile(), fileName);
        SysOss oss = new SysOss();
        oss.setOssId(IdUtil.getSnowflakeNextId());
        oss.setUrl(url);
        oss.setFileSuffix(suffix);
        oss.setFileName(fileName);
        oss.setOriginalName(originalFileName);
        oss.setService(HuaweiyunOss.CONFIG_KEY);
        oss.setCreateTime(new Date());
        oss.setUpdateTime(new Date());
        this.insert(oss);
        return oss;
    }

    @Override
    public List<SysOss> uploads(List<TempFile> files) {
        return null;
    }

    @Override
    public SysOss upload(File file) {
        String originalFileName = file.getName();
        String suffix = StringUtils.substring(originalFileName, originalFileName.lastIndexOf("."), originalFileName.length());
        String fileName = HuaweiyunOss.getObjectKey(suffix);
        //上传文件
        String url = huaweiyunOss.upload(file, fileName);
        SysOss oss = new SysOss();
        oss.setOssId(IdUtil.getSnowflakeNextId());
        oss.setUrl(url);
        oss.setFileSuffix(suffix);
        oss.setFileName(fileName);
        oss.setOriginalName(originalFileName);
        oss.setService(HuaweiyunOss.CONFIG_KEY);
        oss.setCreateTime(new Date());
        oss.setUpdateTime(new Date());
        this.insert(oss);
        return oss;
    }

    @Override
    public void download(Long ossId, HttpServletResponse response) throws IOException {
        SysOss sysOss = this.getById(ossId);
        if (ObjectUtil.isNull(sysOss)) {
            throw new ServiceException("文件数据不存在!");
        }
        InputStream inputStream = huaweiyunOss.download(sysOss.getFileName());
        int available = inputStream.available();
        IoUtil.copy(inputStream, response.getOutputStream(), available);
        response.setContentLength(available);
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        return null;
    }

}
